using GameFramework.Network;

namespace LS.UGFUnits
{
    using Unity.VisualScripting;
    
    public class CreateNetworkChannel: NetworkUnit
    {
        [DoNotSerialize]
        public ValueInput Name { get; private set; }
        
        [DoNotSerialize]
        public ValueInput ServiceType { get; private set; }

        [DoNotSerialize]
        public ValueInput ChannelHelper { get; private set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueOutput Channel { get; private set; }

        protected override void Definition()
        {
            base.Definition();
            Name = ValueInput<string>(nameof(Name), string.Empty);
            ServiceType = ValueInput<ServiceType>(nameof(ServiceType), GameFramework.Network.ServiceType.Tcp);
            ChannelHelper = ValueInput<INetworkChannelHelper>(nameof(ChannelHelper));
            Channel = ValueOutput<INetworkChannel>(nameof(Channel));
            
            Requirement(Name, Input);
            Requirement(ServiceType, Input);
            Requirement(ChannelHelper, Input);
        }

        protected override ControlOutput In(Flow flow)
        {
            string name = flow.GetValue<string>(Name);
            ServiceType serviceType = flow.GetValue<ServiceType>(ServiceType);
            INetworkChannelHelper channelHelper = flow.GetValue<INetworkChannelHelper>(ChannelHelper);
            var channel = Target?.CreateNetworkChannel(name, serviceType, channelHelper);
            flow.SetValue(Channel, channel);
            return base.In(flow);
        }
    }
}